package com.medusa.aps.business.modules.basic.controller;


import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.basic.model.input.MaterialUnitDto;
import com.medusa.aps.business.modules.basic.model.input.MaterialUnitParam;
import com.medusa.aps.business.modules.basic.mp.entity.MaterialUnit;
import com.medusa.aps.business.modules.basic.mp.service.MaterialUnitService;
import com.medusa.aps.business.common.model.resp.Result;
import com.medusa.aps.business.common.web.valid.group.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物料单位控制层
 *
 * @author yuxh
 * @since 2024-02-03 16:20:25
 */
@Validated
@RestController
@RequestMapping("/materialUnit")
@Tag(name = "物料单位管理", description = "物料单位管理")
public class MaterialUnitController {
    /**
     * 服务对象
     */
    @Resource
    private MaterialUnitService materialUnitService;

    /**
     * 分页查询物料单位
     * @param materialUnitParam 查询参数
     * @return 分页对象
     */
    @GetMapping("/selectAll")
    @Operation(summary = "分页查询所有数据")
    public Result<Page<MaterialUnit>> selectAll(@Parameter(description = "查询参数") MaterialUnitParam materialUnitParam) {
        return Result.ok(materialUnitService.selectAll(materialUnitParam));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    @Operation(summary = "通过主键查询单条数据")
    public Result<MaterialUnit> selectOne(@RequestParam Integer id) {
        return Result.ok(materialUnitService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param materialUnitDtos 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    @Operation(summary = "新增数据")
    public Result<Void> saveMaterialUnit(@RequestBody @Valid @Size(min = 1, max = 50, message = "物料单位最多添加50条") List<MaterialUnitDto> materialUnitDtos) {
        materialUnitService.saveMaterialUnit(materialUnitDtos);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param materialUnitDtos 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "修改数据")
    public Result<Boolean> updateMaterialClassification(@RequestBody @Validated(Update.class) MaterialUnitDto materialUnitDtos) {
        return Result.ok(materialUnitService.updateMaterialUnit(materialUnitDtos));
    }

    /**
     * 删除数据
     *
     * @param ids id集合
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除数据")
    public Result<Void> delete(@RequestBody List<Long> ids) {
        materialUnitService.deleteMaterialUnit(ids);
        return Result.ok();
    }
}

